package swimv2.session;

import java.util.ArrayList;
import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.NamedQuery;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import javax.ejb.Stateless;
import javax.persistence.Query;

import org.jboss.ejb3.annotation.RemoteBinding;

import swimv2.entity.Ability;
import swimv2.entity.Friendship;
import swimv2.entity.Notification;
import swimv2.entity.User;

/**
 * Session Bean implementation class FriendshipBean
 */
@Stateless
@RemoteBinding( jndiBinding = "IFriendshipRemote" )
public class FriendshipBean implements IFriendshipRemote {
	@PersistenceContext(unitName = "swimv2")
	protected EntityManager manager;
	 
    /**
     * Default constructor. 
     */
    public FriendshipBean() {
        // TODO Auto-generated constructor stub
    }

	@Override
	public boolean isFriend(Integer id_amico, Integer id_utente) {
		
		Query q= manager.createQuery("SELECT f FROM Friendship f WHERE f.receiver = :r and f.sender = :s");
		q.setParameter( "s", manager.find(User.class,id_utente));	
		q.setParameter( "r", manager.find(User.class,id_amico));	
		
		if(q.getResultList().isEmpty())return false;
		else return true;
	}

	@Override
	public boolean requestFriendship(Integer id_amico, Integer id_utente) {
		
		Friendship f = new Friendship();

		f.setReceiver(manager.find(User.class,id_amico));
			
		f.setSender(manager.find(User.class,id_utente));
		
		manager.persist(f);
		return true;
	}

	@Override
	public boolean acceptFriendship(Integer id_amico, Integer id_utente) {
		
		Friendship f = new Friendship();
		
		f.setReceiver(manager.find(User.class,id_utente));
		
		f.setSender(manager.find(User.class,id_amico));
		
		manager.persist(f);
		return true;
	}

	@Override
	public List<User> listFriend(Integer id_utente) {
		//non funziona
		List<User> lista = listaAmici(id_utente);
		
		return lista;
	}

	/**
	 * @param id_utente
	 * @return
	 */
	private List<User> listaAmici(Integer id_utente) {
		
		Query q= manager.createQuery("SELECT f FROM Friendship f WHERE f.sender = :nome");
		q.setParameter( "nome", manager.find(User.class, id_utente));
		
		List<Friendship> amici= (List<Friendship>)q.getResultList();
		
		List<User> lista= new ArrayList();
		
		for(int i=0; i<amici.size(); i++)
			lista.add(amici.get(i).getReceiver());
		return lista;
	}

	@Override
	public List<User> searchFriend(String nome, Integer id_utente) {
		
		List<User> amici= new ArrayList();
		
		List<User> lista = listaAmici(id_utente);
		
		for(int i=0; i<lista.size(); i++) {
			if(lista.get(i).getName()==nome)
				amici.add(lista.get(i));
		}
		
		return amici;
	}
	
	@Override
	public User searchFriend(Integer id_amico, Integer id_utente) {
		
		Query q= manager.createQuery("SELECT f FROM Friendship f WHERE f.receiver = :r and f.sender = :s");
		q.setParameter( "r", manager.find(User.class,id_amico));	
		q.setParameter( "s", manager.find(User.class,id_utente));		
		
		Friendship amico = (Friendship)q.getSingleResult();
		return amico.getSender();
	}

	@Override
	public boolean removeFriend(Integer id_amico, Integer id_utente) {

		Query q= manager.createQuery("SELECT f FROM Friendship f WHERE f.receiver = :r and f.sender = :s");
		q.setParameter( "s", manager.find(User.class,id_amico));	
		q.setParameter( "r", manager.find(User.class,id_utente));	
		manager.remove((Friendship)q.getResultList().get(0));
		
		q= manager.createQuery("SELECT f FROM Friendship f WHERE f.receiver = :r and f.sender = :s");
		q.setParameter( "s", manager.find(User.class,id_utente));	
		q.setParameter( "r", manager.find(User.class,id_amico));	
		manager.remove((Friendship)q.getResultList().get(0));
		return true;
	}

	@Override
	public List<User> getRequest(Integer id_utente) {
		Query q= manager.createQuery("SELECT f FROM Friendship f WHERE f.receiver = :r and f.sender NOT IN (select f1 FROM Friendship f1 WHERE f1.receiver = f.sender and f1.sender = :r)");
		q.setParameter( "r", manager.find(User.class,id_utente));		
		
		List<Friendship> f= (List<Friendship>)q.getResultList();
		
		List<User> richieste= new ArrayList<User>();
		
		for(int i=0; i<f.size(); i++)
			richieste.add(f.get(i).getSender());
		
		return richieste;
	}

	@Override
	public boolean delayRequestFriendship(Integer id_utente, Integer id_nonAmico) {
		
		Query q= manager.createQuery("SELECT f FROM Friendship f WHERE f.receiver = :r and f.sender = :s");
		q.setParameter( "s", manager.find(User.class,id_utente));	
		q.setParameter( "r", manager.find(User.class,id_nonAmico));	
		
		manager.remove(q.getResultList().get(0));
		
		return true;
	}
	

}
